//
//  ZHXIntroductionViewController.m
//  FitTime
//
//  Created by 赵建平 on 16/4/9.
//  Copyright © 2016年 赵建平. All rights reserved.
//

#import "ZHXIntroductionViewController.h"
#import <MediaPlayer/MediaPlayer.h>


@interface ZHXIntroductionViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong) MPMoviePlayerController *moviePlayer;

/**
 *  滚动视图
 */
@property (nonatomic,strong) UIScrollView *scrollView;
/**
 *  页码
 */
@property (nonatomic,strong) UIPageControl *pageControl;

/**
 *  定时器
 */
@property (nonatomic,strong) NSTimer *timer;

/**
 *  图片数量
 */
@property (nonatomic,assign) int imageTotal;

@end

@implementation ZHXIntroductionViewController

- (void)viewDidLoad {
     [super viewDidLoad];
   
     [self create];
     [self createLableButton];
     [self createPlayer];
    
}

- (void)createLableButton {
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 400, self.view.frame.size.width, 50)];
    lable.text = @"Show Time";
    lable.textAlignment = NSTextAlignmentCenter;
    lable.textColor = [UIColor whiteColor];
    lable.font = [UIFont systemFontOfSize:50 weight:2];
    [self.view addSubview:lable];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button setTitle:@"注 册" forState:UIControlStateNormal];
    button.frame = CGRectMake(self.view.frame.size.width / 5, self.view.frame.size.height - 100, self.view.frame.size.width / 5, 30);
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setFont:[UIFont systemFontOfSize:20 weight:1]];
    button.layer.borderWidth = 1.0f;
    button.layer.borderColor = [UIColor whiteColor].CGColor;
    
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setTitle:@"登 陆" forState:UIControlStateNormal];
    button1.frame = CGRectMake(self.view.frame.size.width / 5 * 3, self.view.frame.size.height - 100, self.view.frame.size.width / 5, 30);
    button1.layer.borderWidth = 1.0f;
    button1.layer.borderColor = [UIColor whiteColor].CGColor;
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button1 setFont:[UIFont systemFontOfSize:20 weight:1]];
    [self.view addSubview:button1];
}


- (void)create {
    //1 设置 image
    //图片位置
    CGFloat imageH = self.scrollView.frame.size.height;
    CGFloat imageW = self.scrollView.frame.size.width;
    CGFloat imageX = 0;
    CGFloat imageY = 0;
    
    NSArray *array = @[@"分享汗水后你的性感", @"规划陪伴你的训练过程", @"全程记录你的健身数据", @"每个动作都精确规范"];
    
    for (int i=0; i< self.imageTotal; i++) {
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(_scrollView.bounds.size.width * i, _scrollView.frame.size.height - 200, self.view.frame.size.width - 30, 30)];
        lable.text = array[i];
        lable.font = [UIFont systemFontOfSize:20 weight:1];
        lable.textColor = [UIColor whiteColor];
        lable.textAlignment = NSTextAlignmentCenter;
        [self.scrollView addSubview:lable];
        }

    //隐藏进度条
    self.scrollView.showsHorizontalScrollIndicator=NO;
    
    //设置拖拽范围
    CGFloat sizeW = self.imageTotal*imageW;
    self.scrollView.contentSize =CGSizeMake(sizeW, 0);
    self.scrollView.pagingEnabled=YES;
    self.scrollView.delegate=self;
    
    //3 设置 pageControl
    //设置共用多少页
    self.pageControl.numberOfPages= self.imageTotal;
    
    //设置其他页码颜色 绿色
    self.pageControl.pageIndicatorTintColor = [UIColor greenColor];
    
    //设置当前页码颜色 红色
    self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    
    //4.添加定时器
    [self addTimer];
}

#pragma mark - 图片调用方法
/**
 *  下一张图片
 */
-(void)nextImage
{
    NSInteger page = self.pageControl.currentPage;
    if (page==(self.imageTotal-1)) {
        page=0;
    } else {
        page++;
    }
    self.scrollView.contentOffset = CGPointMake(page*self.scrollView.frame.size.width, 0);
}

#pragma mark - timer方法
-(void)addTimer
{
    self.timer =  [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    //多线程 UI IOS程序默认只有一个主线程，处理UI的只有主线程。如果拖动第二个UI，则第一个UI事件则会失效。
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}


/**
 *  关闭定时器
 */
-(void)closeTimer
{
    [self.timer invalidate];
}

#pragma mark - scrollView事件
//  scrollView 开始拖拽的时候调用
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self closeTimer];
}


// scrollView滚动的时候调用
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //    分页计算方法
    //    当前页=(scrollView.contentOffset.x+scrollView.frame.size.width/2)/scrollView.frame.size.width
    CGFloat page = (scrollView.contentOffset.x+scrollView.frame.size.width/2)/(scrollView.frame.size.width);
    self.pageControl.currentPage=page;
    
}

// scrollView 结束拖拽的时候调用
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addTimer];
}

#pragma mark - 懒加载
-(UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]init];
        
        CGFloat scrollW = self.view.frame.size.width;
        CGFloat scrollH = self.view.frame.size.height;
        CGFloat scrollX = 0;
        CGFloat scrollY = 0;
        _scrollView.frame = CGRectMake(scrollX, scrollY, scrollW, scrollH);
        _scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 3, 0);
        _scrollView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}
-(UIPageControl *)pageControl
{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc]init];
        _pageControl.contentHorizontalAlignment =UIControlContentHorizontalAlignmentCenter;
        _pageControl.frame =CGRectMake(0, self.view.frame.size.height - 150, self.view.frame.size.width, 10);
        [self.view addSubview:_pageControl];
    }
    return _pageControl;
}

-(int)imageTotal
{
    return 4;
}

#pragma mark - 创建视频播放
- (void)createPlayer {
    NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"splash" ofType:@"mp4"];
    
    NSURL *url = [NSURL fileURLWithPath:urlStr];
    self.moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
    self.view.frame = self.moviePlayer.view.frame;
    
    _moviePlayer.controlStyle = MPMovieControlStyleNone;
    [self.view addSubview:self.moviePlayer.view];
    
    [self.view sendSubviewToBack:self.moviePlayer.view];
    
    self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    // 重新播放
    self.moviePlayer.repeatMode = YES;
    [self.moviePlayer play];
    
    /*
     NSLog(@"%f缓冲时间", self.moviePlayer.duration);
     NSLog(@"%f总时间", self.moviePlayer.currentPlaybackTime);
     NSLog(@"%f当前播放时间", self.moviePlayer.playableDuration);
     */
    
}

#pragma mark - button点击事件
- (void)buttonAction:(UIButton *)sender {
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"changeRootVC" object:nil userInfo:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
